using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.Settings;

namespace THANHTUNGO.Portal.Components.UI
{
    public class PortalModuleControl : UserControl
    {
        //-----------------------------------------------------------------------------------------
        private Module _Module;
        private Hashtable _Settings;
        public ModuleTitle ModuleTitle;

        #region Tab

        private int _tabID = 0;
        private int _tabIndex = 0;

        public int TabID
        {
            get
            {
                if (this._tabID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TabID"] != null)
                        this._tabID = Int32.Parse(Request.Params["TabID"]);
                    else
                        this._tabID = 1;
                }
                return this._tabID;
            }
        }

        public int TabIndex
        {
            get
            {
                if (this._tabIndex == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TabIndex"] != null)
                        this._tabIndex = Int32.Parse(Request.Params["TabIndex"]);
                }
                return this._tabIndex;
            }
        }

        #endregion

        #region Module

        public int ModuleID
        {
            get { return this._Module.ID; }
        }

        public Guid GeneralModuleDefinitionID
        {
            get { return this._Module.GeneralDefinitionID; }
        }
        #endregion

        #region Portal

        private int _portalId = 0;

        public PortalSettings PortalSettings
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    return (PortalSettings) HttpContext.Current.Items["PortalSettings"];
                }
                return null;
            }
        }

        public int PortalID
        {
            get { return _portalId; }
            set { _portalId = value; }
        }

        #endregion

        #region Permission
        
        private int _isViewable = 0;
        public bool IsViewable
        {
            get
            {
                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)
                if (_Module == null || _Module.AuthorizedViewRoles == null)
                    return false;

                if (_isViewable == 0)
                {
                    // Obtain PortalSettings from Current Context
                    if (PortalSecurity.IsInRoles(_Module.AuthorizedViewRoles))
                    {
                        _isViewable = 1;
                    }
                    else
                    {
                        _isViewable = 2;
                    }
                }

                return (_isViewable == 1);
            }
        }
        //-----------------------------------------------------------------------------------------

        private int _isAddable = 0;
        public bool IsAddable
        {
            get
            {
                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (_isAddable == 0)
                {
                    // Obtain PortalSettings from Current Context
                    PortalSettings pSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];

                    if (pSettings.AlwaysShowEditButton && PortalSecurity.IsInRoles(_Module.AuthorizedAddRoles))
                    {
                        _isAddable = 1;
                    }
                    else
                    {
                        _isAddable = 2;
                    }
                }

                return (_isAddable == 1);
            }           
        }
        //-----------------------------------------------------------------------------------------

        private int _isEditable = 0;
        public bool IsEditable
        {
            get
            {
                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (_isEditable == 0)
                {
                    // Obtain PortalSettings from Current Context
                    PortalSettings pSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];

                    if (pSettings.AlwaysShowEditButton && PortalSecurity.IsInRoles(_Module.AuthorizedEditRoles))
                    {
                        _isEditable = 1;
                    }
                    else
                    {
                        _isEditable = 2;
                    }
                }

                return (_isEditable == 1);
            }
        }
        //-----------------------------------------------------------------------------------------

        private int _isDeleteable = 0;
        public bool IsDeleteable
        {
            get
            {
                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (_isDeleteable == 0)
                {
                    // Obtain PortalSettings from Current Context
                    PortalSettings pSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];

                    if (pSettings.AlwaysShowEditButton && PortalSecurity.IsInRoles(_Module.AuthorizedDeleteRoles))
                    {
                        _isDeleteable = 1;
                    }
                    else
                    {
                        _isDeleteable = 2;
                    }
                }

                return (_isDeleteable == 1);
            }
        }

        //-----------------------------------------------------------------------------------------

        private int _isManagerable = 0;
        public bool IsManagerable
        {
            get
            {
                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (_isManagerable == 0)
                {
                    // Obtain PortalSettings from Current Context
                    PortalSettings pSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];

                    if (pSettings.AlwaysShowEditButton && PortalSecurity.IsInRoles(_Module.AuthorizedManagerRoles))
                    {
                        _isManagerable = 1;
                    }
                    else
                    {
                        _isManagerable = 2;
                    }
                }

                return (_isManagerable == 1);
            }
        }

        #endregion

        public Module Module
        {
            get { return _Module; }
            set { _Module = value; }
        }

        public Hashtable Settings
        {
            get
            {
                if (_Settings == null)
                {
                    _Settings = Configuration.GetModuleSettings(ModuleID);
                }

                return _Settings;
            }
        }

        public virtual Guid GuidID
        {
            get
            {
                throw new NotImplementedException("You must implement a unique GUID for your module");
            }
        }
        

        #region Event

        protected override void OnInit(EventArgs e)
        {
            this.ModuleTitle = (ModuleTitle)this.LoadControl("~/ModuleTitle.ascx");
            this.ModuleTitle.SecurityUrl = "~/Modules/MODULE/ModuleSecurity.aspx?tabid=" + this.TabID + "&mid=" + this.ModuleID;
            this.ModuleTitle.PropertyUrl = "~/Modules/MODULE/ModuleSetting.aspx?tabid=" + this.TabID + "&mid=" + this.ModuleID;
            this.Controls.AddAt(0, this.ModuleTitle);
            base.OnPreRender(e);
        }

        #endregion
    }
}